var util = require('../../utils/util.js');
var api = require('../../config/api.js');
Page({
  data: {
    id: 0,
    name: '',
    mechanismName: '',
    discountsAddTime: '',
    discountsEndTime: '',
    buyAddTime: '',
    buyEndTime: '',
    retailPrice: '',
    number: 0,
    picUrl: '',
    desc: ''
  },
  onShow: function () {
    let that = this;
    util.request(api.VoucherDetail, {
      id: that.data.id
    })
      .then(function (res) {
        that.setData({
          name: res.data.goods.name,
          mechanismName: res.data.goods.mechanismName,
          discountsAddTime: res.data.goods.discountsAddTime,
          discountsEndTime: res.data.goods.discountsEndTime,
          buyAddTime: res.data.goods.buyAddTime,
          buyEndTime: res.data.goods.buyEndTime,
          retailPrice: res.data.goods.retailPrice,
          number: res.data.goods.number,
          picUrl: res.data.goods.picUrl,
          desc: res.data.goods.desc
        });
      });
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      id: parseInt(options.id)
    });
  },
  buyNow: function () {
    var endTime = Date.parse(new Date()) - Date.parse(this.data.buyEndTime)
    var addTime = Date.parse(new Date()) - Date.parse(this.data.buyAddTime)
    if(addTime >= 0 && endTime <= 0){
      wx.navigateTo({
        url: '/pages/buyVoucherDetail/buyVoucherDetail?id=' + this.data.id,
      })
    }
    if(endTime > 0 || addTime < 0){
      wx.showModal({
        title: '提示',
        content: '当前时间不在购买时间内！',
        showCancel: false
      })
    }
  }
})
